package bysj.service;

import bysj.dao.OpenAreaDao;
import bysj.domain.OpenArea;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class OpenAreaService {
    private static OpenAreaDao openAreaDao
            = OpenAreaDao.getInstance();
    private static OpenAreaService openAreaService = null;
    private OpenAreaService(){}

    public static OpenAreaService getInstance(){
        if (openAreaService == null){
            openAreaService =new OpenAreaService();
        }
        return openAreaService;
    }

    public Collection<OpenArea> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<OpenArea> openAreas = openAreaDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return openAreas;
    }

    public OpenArea find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        OpenArea openArea = openAreaDao.find(id,connection);
        JdbcHelper.close(connection);
        return openArea;
    }

    public boolean update(OpenArea openArea) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = openAreaDao.update(openArea,connection);
        JdbcHelper.close(connection);
        return updated;
    }

    public boolean add(OpenArea openArea) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = openAreaDao.add(openArea,connection);
        JdbcHelper.close(connection);
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = openAreaDao.delete(id,connection);
        JdbcHelper.close(connection);
        return deleted;
    }
}
